package db;

import java.sql.Connection;
import java.util.List;

public class StudentDao extends BaseDAO<Student> {
    private Connection connection = null;
    public StudentDao() {
        try {
            this.connection = JDBCUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Student> getStudents() {
        String sql = "select id, name, student_id studentId, major, class clasz from Student";
        return super.getForList(connection, sql);
    }

    public void removeStudentById(String id) {
        String sql = "delete from Student where id = ?";
        super.update(connection, sql, id);
    }

    public int addStudent(Student student) {
        String sql = "INSERT INTO student(NAME, student_id, major, class )" + "VALUES(?,?,?,?)";
        super.update(connection, sql, student.name, student.studentId, student.major, student.clasz);
        List<Student> students = getStudents();
        return students.get(students.size()-1).id;
    }
}
